package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

public class AResultRecord extends AxdrType {
    private byte[] dataCode = null;
    private OAD oad = null;
    private RCSD rcsd = null;
    private SubChoiceRespData respData = null;

    public AResultRecord() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public OAD getOad() {
        return oad;
    }

    public RCSD getRcsd() {
        return rcsd;
    }

    public SubChoiceRespData getRespData() {
        return respData;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OAD oad, RCSD rcsd, SubChoiceRespData respData) {
        this.oad = oad;
        this.rcsd = rcsd;
        this.respData = respData;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        oad = new OAD();
        codeLength += oad.decode(input);
        rcsd = new RCSD();
        codeLength += rcsd.decode(input);
        respData = new SubChoiceRespData();
        respData.setLength(rcsd.size());
        codeLength += respData.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (dataCode != null) {
            output.writeByte(dataCode);
            codeLength = dataCode.length;
        } else {
            if (respData != null && rcsd != null && oad != null) {
                codeLength = 0;
                codeLength += respData.encode(output);
                codeLength += rcsd.encode(output);
                codeLength += oad.encode(output);
            } else {
                throw new Exception("respData || rcsd || oad is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        dataCode = revOStream.getArray();
    }

    public String toString() {
        if (oad != null && rcsd != null && respData != null) {
            return "sequence: {" + "oad: " + oad + ", rcsd: " + rcsd + ", respData: " + respData + "}";
        } else {
            return "respData || rcsd || oad is null";
        }
    }

}
